package com.seasonsfood.mall.service.dispatch.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.seasonsfood.mall.core.service.impl.BaseServiceImpl;
import com.seasonsfood.mall.service.dispatch.api.domain.DispArea;
import com.seasonsfood.mall.service.dispatch.api.model.AreaTreeModel;
import com.seasonsfood.mall.service.dispatch.api.service.DispAreaService;
import com.seasonsfood.mall.service.dispatch.mapper.DispAreaMapper;
import com.seasonsfood.mall.util.constant.ResponseCode;
import com.seasonsfood.mall.util.model.ListResponse;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component("dispAreaService")
@Service(interfaceClass = DispAreaService.class)
public class DispAreaServiceImpl extends BaseServiceImpl<DispArea> implements DispAreaService {
    @Resource
    DispAreaMapper dispAreaMapper;

    @Override
    public ResponseCode addDispArea(DispArea dispArea) {

        Long pid = dispArea.getParentId();
        DispArea parentArea = dispAreaMapper.selectByPrimaryKey(pid);

        // 如果没有父节点，设置父节点为0
        if (parentArea == null) {
            dispArea.setParentId(0L);
            dispArea.setParentIds("0");
        } else {
            dispArea.setParentIds(parentArea.getParentIds() + "/" + parentArea.getId());
        }
        // 设置区域类型 1.省 2.市 3.区/县 4.小区
        dispArea.setAreaType(dispArea.getParentIds().split("/").length);

        dispArea.setCreateTime(new Date());
        int count = dispAreaMapper.insertSelective(dispArea);
        if (count <= 0) {
            return ResponseCode.ADD_ERROR;
        }
        return ResponseCode.SUCCESS;
    }

    @Override
    public ListResponse<DispArea> getAreaList(Long id) {
        Example example = new Example(DispArea.class);
        Example.Criteria criteria = example.createCriteria();
        if (id != null) {
            criteria.andEqualTo("parentId", id);
        }
       example.selectProperties("id");
        example.selectProperties("areaName");
        example.selectProperties("areaType");
        example.selectProperties("childAreaTotalNum");
        example.selectProperties("currentAreaRegisterUserNum");
        example.selectProperties("isHasDisp");
        PageInfo<DispArea> pageInfo = new PageInfo<>(dispAreaMapper.selectByExample(example));
        ListResponse<DispArea> listResponse = new ListResponse<>(pageInfo);
        return listResponse;
    }


    @Override
    public ListResponse<DispArea> getParentList(Integer areaType) {
        Example example = new Example(DispArea.class);
        Example.Criteria criteria = example.createCriteria();
        if (areaType != null && areaType > 0) {
            areaType -= 1;
            criteria.andEqualTo("areaType", areaType);
        }
        example.selectProperties("id");
        example.selectProperties("areaName");
        example.selectProperties("areaType");
        example.selectProperties("childAreaTotalNum");
        example.selectProperties("currentAreaRegisterUserNum");
        example.selectProperties("isHasDisp");
        PageInfo<DispArea> pageInfo = new PageInfo<>(dispAreaMapper.selectByExample(example));
        ListResponse<DispArea> listResponse = new ListResponse<>(pageInfo);
        return listResponse;
    }

    @Override
    public List<AreaTreeModel> getDispAreaTree(Integer rank) {

        List<DispArea> areaList = dispAreaMapper.selectAll();
        List<AreaTreeModel> areaTree = new ArrayList<>();
        areaList.forEach(area -> {
            // 如果当前节点层级小于等于要显示的层级，则将该节点装进树结构中
            if (rank == null || area.getAreaType() <= rank) {
                AreaTreeModel areaTreeModel = new AreaTreeModel(area.getId(), area.getParentId(), area.getAreaName(), area.getAreaType(), area.getIsHasDisp());
                areaTree.add(areaTreeModel);
            }
        });
        return areaTree;

    }


    @Override
    public ListResponse<DispArea> getStreetList(String streetName,Integer parentId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(DispArea.class);
        Example.Criteria criteria = example.createCriteria();
        if (streetName != null) {
            String inputName = "%" + streetName + "%";
            criteria.andLike("areaName", inputName);
        }
        if(parentId==null){
           parentId=5;
        }
        criteria.andEqualTo("parentId", parentId);
        criteria.andEqualTo("areaType", DispArea.TYPE_STREET);
        criteria.andEqualTo("isHasDisp", 1);

        PageInfo<DispArea> pageInfo = new PageInfo<>(dispAreaMapper.selectByExample(example));
        return new ListResponse(pageInfo);
    }
}
